package zozopo.warehouse;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.WarehouseOS;
import zozopo.warehouse.operations.WarehouseTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class AddWarehouseAction extends ActionSupport implements Action,SessionAware{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
	
		private static final String limitedCode ="WAREHOUSE.WAREHOUSE.ADD";
	
		//传入参数
		private String name_warehouse;
		private String address_warehouse;
		private String phone_warehouse;
		private String mobile_warehouse;
		private String qq_warehouse;
		private String manager_warehouse;
	

		private String remark_warehouse;
		
		//传出参数
		private WarehouseTB warehouse;
		
		private Map<Object, Object> session;
		public void setSession(Map arg0) {
			// TODO Auto-generated method stub
			session = arg0;
		}
		public String execute(){
			
			//权限处理
	
			OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
			//用户权限
			Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
			
			if(operator == null){
				addFieldError("errormessage", "用户未登陆或超时.请登陆后操作.");
				return NONE;
			}
			
			WarehouseOS warehouseOS = new WarehouseOS();
			
			warehouse = new WarehouseTB();
			
			warehouse.setAddress_warehouse(address_warehouse);
			warehouse.setManager_warehouse(manager_warehouse);
			warehouse.setMobile_warehouse(mobile_warehouse);
			warehouse.setName_warehouse(name_warehouse);
			warehouse.setPhone_warehouse(phone_warehouse);
			warehouse.setQq_warehouse(qq_warehouse);
			warehouse.setRemark_warehouse(remark_warehouse);
			
			warehouse.setUseremail_warehouse(operator.getEmail_operator());
			warehouse.setUsername_warehouse(operator.getName_operator());
			
			
			warehouseOS.insertWarehouse(warehouse);
			
			warehouse = warehouseOS.getWarehouseById(warehouseOS.getMaxIdByEmail(operator.getEmail_operator())+"");
			
			warehouseOS.releaseConnection();
			
			return SUCCESS;
			
		}
		
		public void validate() {
			
			//权限处理
			OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
		
			if(operator == null){
				addFieldError("errormessage",getText("operator.timeoutornotlogin"));
				return ; 
			} 
			
			//用户权限
			Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
			
			if(operatorlimitedMap.get(limitedCode) == null){
				addFieldError("errormessage","用户没有添加添库权限.");
				return;
			}
		} 
		
		
		public String getName_warehouse() {
			return name_warehouse;
		}
		public void setName_warehouse(String name_warehouse) {
			this.name_warehouse = name_warehouse;
		}
		public String getAddress_warehouse() {
			return address_warehouse;
		}
		public void setAddress_warehouse(String address_warehouse) {
			this.address_warehouse = address_warehouse;
		}
		public String getPhone_warehouse() {
			return phone_warehouse;
		}
		public void setPhone_warehouse(String phone_warehouse) {
			this.phone_warehouse = phone_warehouse;
		}
		public String getMobile_warehouse() {
			return mobile_warehouse;
		}
		public void setMobile_warehouse(String mobile_warehouse) {
			this.mobile_warehouse = mobile_warehouse;
		}
		public String getQq_warehouse() {
			return qq_warehouse;
		}
		public void setQq_warehouse(String qq_warehouse) {
			this.qq_warehouse = qq_warehouse;
		}
		public String getManager_warehouse() {
			return manager_warehouse;
		}
		public void setManager_warehouse(String manager_warehouse) {
			this.manager_warehouse = manager_warehouse;
		}
		public String getRemark_warehouse() {
			return remark_warehouse;
		}
		public void setRemark_warehouse(String remark_warehouse) {
			this.remark_warehouse = remark_warehouse;
		}
		public WarehouseTB getWarehouse() {
			return warehouse;
		}
		public void setWarehouse(WarehouseTB warehouse) {
			this.warehouse = warehouse;
		}
		public static String getLimitedcode() {
			return limitedCode;
		}
}
